# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:android)

platform :android do
  lane :bump_version_code do |options|
    track = options[:track]
    path = '../app/build.gradle'
    re = /versionCode\s+(\d+)/ 

    versionCode = google_play_track_version_codes(
      json_key: "./rainbow-google-play-key.json",
      package_name: "me.rainbow", 
      track: track
    )

    updatedCode = versionCode[0] + 1
  
    s = File.read(path)
    versionCode = s[re, 1].to_i
    s[re, 1] = (updatedCode).to_s
  
    f = File.new(path, 'w')
    f.write(s)
    f.close

    versionCode
  end

  desc "Upload a new version to the Google Play"
  lane :release_version do |options|
    track = options[:track]
    version_code = bump_version_code(track: track)

    gradle(task: 'clean')
    gradle(task: 'bundle', build_type: 'Release')
    upload_to_play_store(
      json_key: "./rainbow-google-play-key.json", 
      track: track
    )
  end

  lane :internal do
    release_version(track: "internal")
  end

  lane :alpha do
    release_version(track: "alpha")
  end
  
  lane :beta do
    release_version(track: "beta")
  end

  lane :production do
    release_version(track: "production")
  end

end
